home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / smaltalk.lha / smalltalk-1.1.1 / ArrayedCollection.st < prev    next >
Text File  |  1991-09-12  |  4KB  |  156 lines

  1. "======================================================================
  2. |
  3. |   ArrayedCollection Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbyrne     25 Apr 89      created.
  34. |
  35. "
  36.  
  37. SequenceableCollection variableSubclass: #ArrayedCollection
  38.                instanceVariableNames: ''
  39.                classVariableNames: ''
  40.                poolDictionaries: ''
  41.                category: nil.
  42.  
  43. ArrayedCollection comment: 
  44. 'My instances are objects that are generally fixed size, and are accessed
  45. by an integer index.  The ordering of my instance''s elements is determined 
  46. externally; I will not rearrange the order of the elements.' !
  47.  
  48. !ArrayedCollection class methodsFor: 'instance creation'!
  49.  
  50. with: element1
  51.     | anArrayedCollection |
  52.     anArrayedCollection _ self new: 1.
  53.     anArrayedCollection at: 1 put: element1.
  54.     ^anArrayedCollection
  55. !
  56.  
  57. with: element1 with: element2
  58.     | anArrayedCollection |
  59.     anArrayedCollection _ self new: 2.
  60.     anArrayedCollection at: 1 put: element1.
  61.     anArrayedCollection at: 2 put: element2.
  62.     ^anArrayedCollection
  63. !
  64.  
  65. with: element1 with: element2 with: element3
  66.     | anArrayedCollection |
  67.     anArrayedCollection _ self new: 3.
  68.     anArrayedCollection at: 1 put: element1.
  69.     anArrayedCollection at: 2 put: element2.
  70.     anArrayedCollection at: 3 put: element3.
  71.     ^anArrayedCollection
  72. !
  73.  
  74. with: element1 with: element2 with: element3 with: element4
  75.     | anArrayedCollection |
  76.     anArrayedCollection _ self new: 4.
  77.     anArrayedCollection at: 1 put: element1.
  78.     anArrayedCollection at: 2 put: element2.
  79.     anArrayedCollection at: 3 put: element3.
  80.     anArrayedCollection at: 4 put: element4.
  81.     ^anArrayedCollection
  82. !!
  83.  
  84.  
  85.  
  86. !ArrayedCollection methodsFor: 'basic'!
  87.  
  88. add: value
  89.     self shouldNotImplement
  90. !!
  91.  
  92.  
  93.  
  94. !ArrayedCollection methodsFor: 'comparing'!
  95.  
  96. = anArrayedCollection
  97.     "Return true if the receiver and anArrayedCollection represent arrays
  98.     with equal contents."
  99.     self size = anArrayedCollection size ifFalse: [ ^false ].
  100.     1 to: self size do:
  101.         [ :i | (self at: i) = (anArrayedCollection at: i)
  102.               ifFalse: [ ^false ] ].
  103.     ^true
  104. !
  105.  
  106. hash
  107.     "Return the hash value of the array"
  108.     "### I don't like this hashing algorithm"
  109.     | hashValue |
  110.     hashValue _ 0.
  111.     self do: [ :element | hashValue _ ((hashValue bitShift: 1)
  112.                                         bitXor:  element hash)
  113.                     bitAnd: 16r1FFFFFFF ].
  114.     ^hashValue
  115. !!
  116.  
  117.  
  118. !ArrayedCollection methodsFor: 'printing'!
  119.  
  120. printOn: aStream
  121.     | firstTime |
  122.     aStream nextPutAll: self classNameString.
  123.     aStream nextPutAll: ' ('.
  124.     firstTime _ true.
  125.     self do:
  126.         [ :element | firstTime ifTrue: [ firstTime _ false ]
  127.                                ifFalse: [ aStream nextPut: Character space ].
  128.                      element storeOn: aStream ].
  129.     aStream nextPut: $)
  130. !!
  131.  
  132.  
  133.  
  134. !ArrayedCollection methodsFor: 'storing'!
  135.  
  136. storeOn: aStream
  137.     | index |
  138.     aStream nextPutAll: '(('; nextPutAll: self classNameString; 
  139.         nextPutAll: ' basicNew: '.
  140.     self basicSize printOn: aStream.
  141.     aStream nextPut: $).
  142.     index _ 1.
  143.     self do:
  144.         [ :element | aStream nextPutAll: ' at: '.
  145.                  index printOn: aStream.
  146.              aStream nextPutAll: ' put: '.
  147.                      element storeOn: aStream.
  148.              aStream nextPut: $;.
  149.                      index _ index + 1 ].
  150.     index > 1 ifTrue: [ aStream nextPutAll: ' yourself' ].
  151.     aStream nextPut: $)
  152. !!
  153.  
  154.